// Author: Domien Nowicki

#ifndef MAPWIDGET_H
#define MAPWIDGET_H

#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QCursor>
#include "Map.h"
#include "CellWidget.h"

class MapWidget: public QGraphicsView, public Map
{
	Q_OBJECT
public:
    MapWidget(QWidget *parent=0);
    virtual ~MapWidget();

	virtual void Resize(unsigned int newColumnCount, unsigned int newRowCount);
	virtual void SetWallCell(unsigned int column, unsigned int row);
	virtual void SetEmptyCell(unsigned int column, unsigned int row);
	virtual void SetTextureCell(unsigned int column, unsigned int row, Cell::Side side, unsigned int tindex, const Pixel& userColor);

	CellWidget& GetCellWidget(unsigned int column, unsigned int row);
	const CellWidget& GetCellWidget(unsigned int column, unsigned int row) const;
	void SetCursor(const QCursor& cursor);

	unsigned int GetCellWidgetSize() const;

protected:
	virtual Cell* NewCell(const Cell* copy, unsigned int column, unsigned int row);
	void resizeEvent(QResizeEvent* e);
	void wheelEvent(QWheelEvent *e);
	void keyPressEvent(QKeyEvent *e);
	void scaleView(qreal scaleFactor);
	QRectF boundingRect() const;

signals:
	void clickedCell(unsigned int mapX, unsigned int mapY);
	void clickedCellSide(unsigned int mapX, unsigned int mapY, Cell::Side side);

private slots:
	void slotCellClicked(CellWidget* cell);
	void slotCellSideClicked(CellWidget* cell, Cell::Side side);

private:
	void SetupWidget();
	void Center();

	bool docenter;

	CellWidget *focusCell;

	static const qreal CENTERSCALE;
	static const unsigned int CELLSIZE = 64;
};

#endif // MAPWIDGET_H
